package com.zjw.zy.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

import androidx.annotation.NonNull;

import com.zjw.zy.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 解决滑动冲突的scrollView
 */
public class ConflictScrollView extends ScrollView {

    private final List<View> subViews = new ArrayList<>();

    public ConflictScrollView(Context context) {
        super(context);
    }

    public ConflictScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ConflictScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ConflictScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void addScrollView(@NonNull View view) {
        if (subViews.contains(view)) {
            return;
        }
        subViews.add(view);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        subViews.clear();
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        for (View subView : subViews) {
            if (ViewUtils.hintView(subView, ev)) {
                //出现滑动冲突，此处让ScrollView不可滑动
                return subView.onTouchEvent(ev);
            }
        }
        return super.onTouchEvent(ev);
    }
}
